package xiancheng.deadlock;

/**
 * @author Mr.ma
 * @since 2022/11/1 20:59
 * 演示死锁 画板，画笔
 **/
public class Test09 {
    public static Object board = new Object();
    public static Object brush = new Object();
    public static void main(String[] args) {
        Thread01 one = new Thread01();
        Thread02 two = new Thread02();
        Thread thread01 = new Thread(one);
        Thread thread02 = new Thread(two);
        thread01.setName("小明");
        thread02.setName("小刚");
        thread01.start();
        thread02.start();
    }
}
class Thread01 implements Runnable{
    @Override
    public void run()  {
        synchronized (Test09.board){
            System.out.println(Thread.currentThread().getName() + "拿到画板");
            synchronized (Test09.brush){
                System.out.println(Thread.currentThread().getName() + "拿到画笔");
                System.out.println(Thread.currentThread().getName() + "开始画画");
            }
        }
    }
}
class Thread02 implements Runnable{
    @Override
    public void run() {
        synchronized (Test09.brush){
            System.out.println(Thread.currentThread().getName() + "拿到画笔");
            synchronized (Test09.board){
                System.out.println(Thread.currentThread().getName() + "拿到画板");
                System.out.println(Thread.currentThread().getName() + "开始画画");
            }
        }
    }
}